package top.drinyeit.weilong.presenter;

import java.util.List;

import top.drinyeit.weilong.activity.MessageActivity;
import top.drinyeit.weilong.adapter.MessageAdapter;
import top.drinyeit.weilong.app.Constant;
import top.drinyeit.weilong.model.MessageBean;
import top.drinyeit.weilong.retrofit.HttpResponse;
import top.drinyeit.weilong.retrofit.RetrofitMethod;
import top.drinyeit.weilong.retrofit.subscriber.CommonSubscriber;
import top.drinyeit.weilong.retrofit.subscriber.SubscriberListener;
import top.drinyeit.weilong.util.SPUtil;

import static top.drinyeit.weilong.app.Constant.COMMON_SUCCESS;

/**
 * Created by wu on 2018/1/5.
 */

public class MessagePresenter {
    private MessageActivity activity;

    public MessagePresenter(MessageActivity activity) {
        this.activity = activity;
    }

    //获取消息列表
    public void getMessage(int pageIndex) {
        String user_id = SPUtil.getShareString(Constant.USER_ID);
        if (user_id.equals("")){
            activity.showToastMsg("请先登录");
        }
        activity.showLoading();
        CommonSubscriber<HttpResponse<List<MessageBean>>> subscriber = new CommonSubscriber<>(new SubscriberListener<HttpResponse<List<MessageBean>>>() {
            @Override
            public void onError(String e, int code) {
                activity.shSwipeRefreshLayout.finishRefresh();
                activity.shSwipeRefreshLayout.finishLoadmore();
                activity.dismissLoading();
                activity.showToastMsg("错误，请重新操作！");
            }

            @Override
            public void onNext(HttpResponse<List<MessageBean>> o) {
                activity.shSwipeRefreshLayout.finishRefresh();
                activity.shSwipeRefreshLayout.finishLoadmore();
                activity.dismissLoading();
                if (o.getCode() == COMMON_SUCCESS) {
                    activity.getList().addAll(o.getData());
                    activity.getRecyclerView().getAdapter().notifyDataSetChanged();
                } else {
                    activity.showToastMsg(o.getMessage());
                }

            }
        });
        RetrofitMethod.getInstance().getMessage(subscriber, user_id,pageIndex);
    }

    //删除用户消息
    public void deleteMessage(int message_id, final int position) {
        activity.showLoading();
        CommonSubscriber<HttpResponse<String>> subscriber = new CommonSubscriber<>(new SubscriberListener<HttpResponse<String>>() {
            @Override
            public void onError(String e, int code) {
                activity.dismissLoading();
                activity.showToastMsg("错误，请重新操作！");
            }

            @Override
            public void onNext(HttpResponse<String> o) {
                activity.dismissLoading();
                if (o.getCode() == COMMON_SUCCESS) {
                    ((MessageAdapter) activity.getRecyclerView().getAdapter()).removeItem(position);
                }
            }
        });
        RetrofitMethod.getInstance().deleteMessage(subscriber, message_id);
    }
}
